"""
    @Author  ：思念 
    @File    ：4.进程之间的通信.py
    @Date    ：2025/3/7 11:35 
"""
import time
from multiprocessing import Process, Queue


def write(q):
    print("写入进程启动")
    for i in range(4):
        q.put(i)
        print(f"写入{i}")
        time.sleep(0.5)


def read(q):
    print("读取进程启动")
    while True:
        item = q.get()
        if item is not None:
            print(f"读取{item}")
        else:
            break


if __name__ == '__main__':
    q = Queue()
    p1 = Process(target=write, args=(q,))
    p2 = Process(target=read, args=(q,))
    p1.start()
    p2.start()
    p1.join()
    q.put(None) # 写入None烧哨兵值，表示队列结束
